﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ZQNB.Common;
using ZQNB.Common.Attributes;

namespace ZQNB.Web.Common.Helpers
{
    internal interface INbMetaHelper
    {
        List<MenuAttribute> FindMenuMetas(bool ingoreCache = false);
        List<OpAttribute> FindOpMetas(bool ingoreCache = false);
        List<ModuleAttribute> FindModuleMetas(bool ingoreCache = false);
    }

    public class NbMetaHelper : INbMetaHelper
    {
        private  List<MenuAttribute> _menuAttributes = new List<MenuAttribute>();
        private  List<OpAttribute> _opAttributes = new List<OpAttribute>();
        private  List<ModuleAttribute> _moduleAttributes = new List<ModuleAttribute>();

        //menu
        public List<MenuAttribute> FindMenuMetas(bool ingoreCache = false)
        {
            if (ingoreCache)
            {
                string binPath = MyPathHelper.MakeWebBinDirPath();
                var files = MyIOHelper.GetFiles(binPath, "ZQNB.Web.Areas.*.dll", SearchOption.TopDirectoryOnly);
                var file2 = MyIOHelper.GetFiles(binPath, "ZQNB.Web.dll", SearchOption.TopDirectoryOnly);
                files.AddRange(file2);
                var metaAssemblies = files.Select(Assembly.LoadFrom).ToArray();
                _menuAttributes = AssemblyAttributeHelperTool.FindMetasInMethod<MenuAttribute>(metaAssemblies);
            }
            return _menuAttributes;
            //return FindMenuMetasMock();
        }
        //op
        public List<OpAttribute> FindOpMetas(bool ingoreCache = false)
        {
            if (ingoreCache)
            {
                string binPath = MyPathHelper.MakeWebBinDirPath();
                var files = MyIOHelper.GetFiles(binPath, "ZQNB.Web.Areas.*.dll", SearchOption.TopDirectoryOnly);
                var file2 = MyIOHelper.GetFiles(binPath, "ZQNB.Web.dll", SearchOption.TopDirectoryOnly);
                files.AddRange(file2);
                var metaAssemblies = files.Select(Assembly.LoadFrom).ToArray();
                _opAttributes = AssemblyAttributeHelperTool.FindMetasInMethod<OpAttribute>(metaAssemblies);
            }
            return _opAttributes;
        }
        //module
        public List<ModuleAttribute> FindModuleMetas(bool ingoreCache = false)
        {
            if (ingoreCache)
            {
                string binPath = MyPathHelper.MakeWebBinDirPath();
                var files = MyIOHelper.GetFiles(binPath, "ZQNB.Web.Areas.*.dll", SearchOption.TopDirectoryOnly);
                //var file2 = MyIOHelper.GetFiles(binPath, "ZQNB.Web.dll", SearchOption.TopDirectoryOnly);
                //files.AddRange(file2);
                var metaAssemblies = files.Select(Assembly.LoadFrom).ToArray();
                _moduleAttributes = AssemblyAttributeHelperTool.FindMetasInAssembly<ModuleAttribute>(metaAssemblies);
            }
            return _moduleAttributes;
        }
    }
}
